<template>
    <el-card shadow="never" class="aui-card--fill">
        <div class="mod-guide__guideserveroute}">
            <el-form :inline="true" :model="dataForm" @keyup.enter.native="getalldata()">
                <el-form-item>
                    <el-input v-model="dataForm.name" placeholder="请输入图层名称" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="dataForm.active" clearable placeholder="请选择发布状态">
                        <el-option label="未发布" value="0"></el-option>
                        <el-option label="已发布" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="dataForm.mapGuideId" clearable placeholder="请选择导览图">
                        <el-option v-for="item in datadaolantuList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button @click="getalldata()">{{ $t("query") }}</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button v-if="$hasPermission('sys:ismapguidelayer:save')" type="primary"
                        @click="addnew">新增</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="dataListLoading" :data="dataList" border style="width: 100%">
                <el-table-column prop="name" label="图层名称" min-width="160" :show-overflow-tooltip="true"
                    align="center"></el-table-column>
                <el-table-column prop="sort" label="图层层级" width="120" :show-overflow-tooltip="true"
                    align="center"></el-table-column>
                <el-table-column prop="para" label="图层参数" min-width="100" :show-overflow-tooltip="true"
                    align="center"></el-table-column>
                <el-table-column prop="active" width="100" align="center" :show-overflow-tooltip="true" label="发布状态">
                    <template slot-scope="scope">
                        <span v-if="scope.row.active == 0">未发布</span>
                        <span v-if="scope.row.active == 1">已发布</span>
                    </template>
                </el-table-column>
                <el-table-column prop="createDate" label="创建时间" min-width="110" :show-overflow-tooltip="true"
                    align="center"></el-table-column>
                <el-table-column prop="mapGuideName" label="导览图名称" min-width="150" :show-overflow-tooltip="true"
                    align="center"></el-table-column>
                <el-table-column :label="$t('handle')" fixed="right" align="center" width="150">
                    <template slot-scope="scope">
                        <el-button v-if="$hasPermission('sys:ismapguidelayer:update')" type="text" size="small"
                            @click="editdata(scope.row)">修改
                        </el-button>
                        <el-button v-if="
                            $hasPermission('sys:ismapguidelayer:update') &&
                            scope.row.active == 0
                        " type="text" size="small" @click="Confirmtheorder(scope.row)">发布
                        </el-button>
                        <el-button v-if="
                            $hasPermission('sys:ismapguidelayer:update') &&
                            scope.row.active == 1
                        " type="text" size="small" @click="Confirmnotheorder(scope.row)">取消发布
                        </el-button>
                        <el-button v-if="$hasPermission('sys:ismapguidelayer:delete')" type="text" size="small"
                            @click="deletedata(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle"
                :current-page="dataForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="dataForm.limit"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
            <!-- 弹窗, 新增 / 修改 -->
            <el-dialog :title="tishi" :visible.sync="dialogVisible" width="50%">
                <div class="diadiv">
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
                        <el-form-item label="图层名称" prop="name">
                            <el-input v-model="ruleForm.name"></el-input>
                        </el-form-item>
                        <el-form-item label="图层层级" prop="sort">
                            <el-input v-model="ruleForm.sort"></el-input>
                        </el-form-item>
                        <el-form-item label="图层参数" prop="para">
                            <el-input v-model="ruleForm.para"></el-input>
                        </el-form-item>

                        <el-form-item label="导览图" prop="mapGuideId">
                            <el-select v-model="ruleForm.mapGuideId" placeholder="请选择导览图">
                                <el-option v-for="item in datadaolantuList" :key="item.id" :label="item.name"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="resetForm">取 消</el-button>
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </el-card>
</template>

<script>
export default {
    data() {
        return {
            tishi: "",
            datadaolantuList: [],
            linshidata: {},
            dialogVisible: false,
            dataListLoading: false,
            total: 0,
            dataList: [],
            dataForm: {
                name: "",
                page: 1,
                limit: 10,
                active: "",
                mapGuideId: "",
            },
            ruleForm: {
                name: "",
                sort: "",
                para: "",
                mapGuideId: "",
            },
            rules: {
                name: [{ required: true, message: "请输入图层名称", trigger: "blur" }],
                sort: [{ required: true, message: "请输入图层层级", trigger: "blur" }],
                para: [{ required: true, message: "请输入图层参数", trigger: "blur" }],
                mapGuideId: [
                    { required: true, message: "请选择导览图", trigger: "change" },
                ],
            },
        };
    },
    components: {},
    created() {
        this.getdaolantudata();
    },
    methods: {
        getdaolantudata() {
            this.$http
                .get("/sys/ismapguide/page", {
                    params: {
                        name: "",
                        page: 1,
                        limit: 99999,
                        active: "",
                    },
                })
                .then((res) => {
                    //  console.log('111111111111111111',res)
                    if (res.data.code == 0) {
                        this.datadaolantuList = res.data.data.list;
                        this.getalldata();
                    } else {
                        this.$message.error(res.data.msg);
                    }
                });
        },

        addnew() {
            this.tishi = "新增";
            this.dialogVisible = true;
            this.linshidata = {};
        },

        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    if (this.ruleForm.url == "") {
                        this.$message.error("请添加图层");
                    } else {
                        if (this.tishi == "新增") {
                            this.$http
                                .post("/sys/ismapguidelayer", this.ruleForm)
                                .then((res) => {
                                    if (res.data.code == 0) {
                                        this.$message({
                                            message: "新增成功",
                                            type: "success",
                                        });
                                        this.dialogVisible = false;
                                        this.dataForm.page = 1;
                                        this.getalldata();
                                    } else {
                                        this.$message.error(res.data.msg);
                                    }
                                });
                        } else {
                            this.linshidata.name = this.ruleForm.name;
                            this.linshidata.sort = this.ruleForm.sort;
                            this.linshidata.para = this.ruleForm.para;
                            this.linshidata.mapGuideId = this.ruleForm.mapGuideId;

                            this.$http
                                .put("/sys/ismapguidelayer", this.linshidata)
                                .then((res) => {
                                    if (res.data.code == 0) {
                                        this.$message({
                                            type: "success",
                                            message: "修改成功!",
                                        });
                                        this.dialogVisible = false;
                                        this.dataForm.page = 1;
                                        this.getalldata();
                                    } else {
                                        this.$message.error(res.data.msg);
                                    }
                                });
                        }
                    }
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs.ruleForm.resetFields();
            this.dialogVisible = false;
            this.dataForm.page = 1;
            this.getalldata();
        },
        getalldata() {
            this.dataListLoading = true;
            this.$http
                .get("/sys/ismapguidelayer/page", {
                    params: this.dataForm,
                })
                .then((res) => {
                    this.dataListLoading = false;
                    if (res.data.code == 0) {
                        this.dataList = res.data.data.list;
                        this.total = res.data.data.total;
                    } else {
                        this.$message.error(res.data.msg);
                    }
                });
        },
        pageSizeChangeHandle(val) {
            this.dataForm.limit = val;
            this.getalldata();
        },
        pageCurrentChangeHandle(val) {
            this.dataForm.page = val;
            this.getalldata();
        },

        editdata(e) {
            this.linshidata = {};
            this.tishi = "修改";
            this.ruleForm.name = e.name;
            this.ruleForm.sort = e.sort;
            this.ruleForm.para = e.para;
            this.ruleForm.mapGuideId = e.mapGuideId;
            this.dialogVisible = true;
            this.linshidata = e;
        },

        deletedata(e) {
            this.$confirm("此操作将删除该条数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.$http
                        .delete("/sys/ismapguidelayer", { data: [e.id] })
                        .then((res) => {
                            if (res.data.code == 0) {
                                this.$message({
                                    type: "success",
                                    message: "删除成功!",
                                });
                                this.dialogVisible = false;
                                this.dataForm.page = 1;
                                this.getalldata();
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        });
                })
                .catch(() => { });
        },
        Confirmnotheorder(e) {
            this.$confirm("此操作将取消发布该条数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    e.active = "0";
                    this.$http.put("/sys/ismapguidelayer", e).then((res) => {
                        if (res.data.code == 0) {
                            this.$message({
                                type: "success",
                                message: "取消发布成功!",
                            });
                            this.dialogVisible = false;
                            this.dataForm.page = 1;
                            this.getalldata();
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    });
                })
                .catch(() => { });
        },
        Confirmtheorder(e) {
            this.$confirm("此操作将发布该条数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    e.active = "1";
                    this.$http.put("/sys/ismapguidelayer", e).then((res) => {
                        if (res.data.code == 0) {
                            this.$message({
                                type: "success",
                                message: "发布成功!",
                            });
                            this.dialogVisible = false;
                            this.dataForm.page = 1;
                            this.getalldata();
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    });
                })
                .catch(() => { });
        },
    },
};
</script>
<style lang="scss" scoped>
.diadiv {
    width: 100%;
}
</style>
